<template>
    <div class="edu-page-warp">
        <condition-card @reset="resetQuery" @search="searchData">
            <!--事件-->
            <el-form label-width="130px" label-suffix=":">
            <el-row>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.major.minorGrade')">
                        <el-date-picker
                            v-model="queryParams.grade"
                            value-format="yyyy"
                            type="year"
                            clearable
                            :placeholder="$t('message.plsSelect')"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                    <el-form-item :label="$t('minor.course.advancedLevel')">
                        <DictSelect
                            v-model="queryParams.advancedLevel"
                            :data="options.advancedLevel"
                            clearable
                            style="width:100%;"
                        ></DictSelect>
                    </el-form-item>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item :label="$t('minor.major.college')">
                        <DictSelect
                            filterable
                            v-model="queryParams.college"
                            :data="options.college"
                            clearable
                            style="width: 100%;"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
            </el-form>
        </condition-card>
        <el-card shadow="never">
            <div class="first_title">{{$t('minor.major.majorListTitle')}}</div>
            <div class="btn">
                <div style="float:left;">
                    <el-input
                        :placeholder="$t('minor.major.inputPlaceholder')"
                        prefix-icon="el-icon-search"
                        v-model="queryParams.majorCodeOrName"
                        style="width:240px;"
                        @change="search"
                        @keyup.enter.native="search"
                    ></el-input>
                </div>
                <!-- <el-button @click="exportFile" type="primary">导入专业</el-button> -->
                <div class="float-right" style="margin: 0 8px;">
                    <el-dropdown
                        trigger="click"
                        @command="handleImportCommand"
                    >
                        <el-button>
                            导入
                            <i class="el-icon-arrow-down el-icon--right" size='medium'></i>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="import">导入</el-dropdown-item>
                            <el-dropdown-item command="download">下载模板</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    <el-upload
                        action="/api/cultureservice/minorMajorSup/importMajorInfoSup"
                        style="display: none;"
                        ref="upload"
                        :file-list="fileList"
                        :on-success="handlerSuccess"
                        :before-upload="beforeAvatarUpload"
                        :on-error="handlerError"
                        accept=".xlsx"
                        :auto-upload="true"
                        :show-file-list="false"
                    >
                    <span slot="trigger" ref="importBtn">导入</span>
                    </el-upload>
                </div>
                <div style="float: right;margin-bottom: 16px">
                    <el-button @click="exportExcel">{{$t('minor.course.exportBtn')}}</el-button>
                    <el-button type="primary" @click="addMajorInfo">{{$t('minor.course.addBtn')}}</el-button>
                    <el-button type="primary" @click="copyInfo">复制</el-button>
                </div>
            </div>
            <EduTable
                :selection="true"
                @page-change="pageChange"
                :pageNum="page.pageNum"
                :pageSize="page.pageSize"
                :data="tableData.list"
                :total="tableData.total_"
                @selection-change="handleSelectionChange"
            >
                <edu-table-column :label="$t('minor.major.minorGrade')">
                    <template slot-scope="scope">{{scope.row.minorGrade}}</template>
                </edu-table-column>
                <edu-table-column :label="$t('minor.major.college')" min-width="105">
                    <template slot-scope="scope">{{scope.row.collegeName}}</template>
                </edu-table-column>
                <edu-table-column :label="'辅修' + $t('minor.major.majorCode')" min-width="105">
                    <template slot-scope="scope">{{scope.row.majorCode}}</template>
                </edu-table-column>
                <edu-table-column :label="'辅修' + $t('minor.major.majorName')" min-width="105">
                    <template slot-scope="scope">{{scope.row.majorNameCn}}</template>
                </edu-table-column>
                <edu-table-column :label="'辅修' + $t('minor.major.majorNameEn')" min-width="135">
                    <template slot-scope="scope">{{scope.row.majorNameEn}}</template>
                </edu-table-column>
                <!-- <edu-table-column :label="$t('minor.course.advancedLevel')">
                    <template slot-scope="scope">{{scope.row.advancedLevelName}}</template>
                </edu-table-column> -->
                <edu-table-column :label="$t('minor.major.beginTerm')">
                    <template slot-scope="scope">{{scope.row.beginTerm}}</template>
                </edu-table-column>
                <edu-table-column :label="$t('minor.major.endTerm')">
                    <template slot-scope="scope">{{scope.row.endTerm}}</template>
                </edu-table-column>
                <edu-table-column label="是否启用" width="90">
                    <template slot-scope="scope">
                        <span>{{validMap[scope.row.isValid]}}</span>
                    </template>
                </edu-table-column>
                <el-table-column :label="$t('message.operating')" width="170">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="viewRecord(scope.row)"
                            size="mini"
                        >{{$t('minor.course.viewBtn')}}</el-button>
                        <el-button
                            type="text"
                            @click="editlRecord(scope.row)"
                            size="mini"
                        >{{$t('minor.course.editBtn')}}</el-button>
                        <el-button
                            type="text"
                            @click="setValid(scope.row)"
                            v-if="validMap[scope.row.isValid]"
                            size="mini"
                            >{{scope.row.isValid == 1 && validMap[0] || validMap[1]}}</el-button>
                        <el-button
                            type="text"
                            @click="deleteRecord(scope.row)"
                            size="mini"
                        >{{$t('minor.course.deleteBtn')}}</el-button>
                    </template>
                </el-table-column>
            </EduTable>
            <edu-dialog :title="dialogTitle" width="60%" ref="addDialog" :appendToBody="false" @close="dialogClose">
                <el-form
                    :model="createMajorForm"
                    ref="createMajorForm"
                    label-width="100px"
                    :rules="majorRules"
                >
                    <el-row>
                        <el-col :span="10">
                            <el-form-item :label="$t('minor.major.minorGrade')" prop="grade">
                                <el-date-picker
                                    :disabled="isUpdate || optionType == 1"
                                    v-model="createMajorForm.grade"
                                    value-format="yyyy"
                                    type="year"
                                    :placeholder="$t('message.plsSelect')"
                                    style="width: 100%"
                                    @change="queryMajorCode"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="10" offset="2">
                            <el-form-item :label="$t('minor.major.college')" prop="college">
                                <DictSelect
                                    filterable
                                    :disabled="isUpdate || optionType == 1"
                                    v-model="createMajorForm.college"
                                    :data="options.college"
                                    :plzSelect="true"
                                    @change="queryMajorCode"
                                    style="width: 100%;"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    <!-- </el-row>
                    <el-row> -->
                        <!-- <el-col :span="10">
                            <el-form-item
                                :label="$t('minor.course.advancedLevel')"
                                prop="advancedLevel"
                            >
                                <DictSelect
                                    :disabled="isUpdate"
                                    v-model="createMajorForm.advancedLevel"
                                    :data="options.advancedLevel"
                                    style="width:100%;"
                                ></DictSelect>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="10">
                            <el-form-item
                                :label="$t('culture.profession.proCode')"
                                prop="majorCode"
                            >
                                <el-input
                                    :disabled="isUpdate || optionType == 1"
                                    v-model="createMajorForm.majorCode"
                                    :placeholder="$t('culture.rules.plsInputProCode')"
                                ></el-input>
                                <!-- <el-select v-model="createMajorForm.majorCode" clearable filterable style="width: 100%">
                                    <el-option
                                        v-for="item in options.majorCodeOption"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select> -->
                            </el-form-item>
                        </el-col>
                    <!-- </el-row>
                    <el-row> -->
                        <el-col :span="10" offset="2">
                            <el-form-item
                                :label="$t('culture.profession.proCodeName')"
                                prop="majorNameCn"
                            >
                                <el-input
                                    :disabled="isUpdate || optionType == 1"
                                    v-model="createMajorForm.majorNameCn"
                                    :placeholder="$t('culture.rules.plsInputProName')"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="10">
                            <el-form-item
                                :label="$t('culture.profession.proCodeNameEn')"
                                prop="majorNameEn"
                            >
                                <el-input
                                    :disabled="isUpdate || optionType == 1"
                                    v-model="createMajorForm.majorNameEn"
                                    :placeholder="$t('culture.rules.plsInputProNameEN')"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    <!-- </el-row>
                    <el-row> -->
                        <el-col :span="10" offset="2">
                            <el-form-item label="辅修学科门类"> 
                                <el-select v-model="createMajorForm.minorSubject" clearable filterable :disabled="isUpdate" style="width: 100%">
                                    <el-option
                                        v-for="item in options.minorSubjects"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="10">
                            <el-form-item  :label="$t('label.lengthSchooling')" prop="lengthSchooling">
                                <el-select :disabled="isUpdate" v-model="createMajorForm.lengthSchooling"
                                  clearable style="width: 100%">
                                    <el-option
                                        v-for="item in options.lengthSchoolList"
                                        :key="item.key"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    <!-- </el-row>
                    <el-row> -->
                        <el-col :span="10" offset="2">
                            <el-form-item :label="$t('minor.major.beginTerm')" prop="beginTerm">
                                <DictSelect
                                    :disabled="isUpdate"
                                    :plzSelect="true"
                                    v-model="createMajorForm.beginTerm"
                                    :data="options.beginSemester"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="10">
                            <el-form-item :label="$t('minor.major.endTerm')" prop="endTerm">
                                <DictSelect
                                    :disabled="isUpdate"
                                    :plzSelect="true"
                                    v-model="createMajorForm.endTerm"
                                    :data="options.endSemester"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <span slot="buttons">
                    <el-button
                        @click="cancelMajorForm"
                        v-if="optionType== 0 || optionType== 1"
                    >{{$t('message.cancel')}}</el-button>
                    <el-button
                        type="primary"
                        @click="submitMajorForm"
                        v-if="optionType== 0 || optionType== 1"
                    >{{$t('minor.course.confirmBtn')}}</el-button>
                    <el-button
                        type="primary"
                        @click="cancelMajorForm"
                        v-if="optionType== 2"
                    >{{$t('minor.course.returnBtn')}}</el-button>
                </span>
            </edu-dialog>
            <div>  
                <edu-dialog title="复制" size="small" :visible.sync="copyDialog.show" :autoHeight="true"
                    :confirmBtnText="$t('common.btn.confirm')" :close-on-click-modal="false" @close="copyDialogClose"
                    @confirm="copyDialogConfirm">
                    <el-form :model="copyDialog.formInfo" ref="copyForm" :rules="copyDialog.rules" label-width="120px"
                        label-position="right" size="small">
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="源专业年级" prop="startMinorGrade">
                                    <el-date-picker v-model="copyDialog.formInfo.startMinorGrade" type="year" style="width:100%"
                                    value-format="yyyy" :placeholder="$t('graduationList.pleaseSelect')">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="18">
                                <el-form-item label="复制到" prop="endMinorGrade">
                                    <el-date-picker v-model="copyDialog.formInfo.endMinorGrade" type="year" style="width:100%"
                                    value-format="yyyy" :placeholder="$t('graduationList.pleaseSelect')">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>    
                        </el-row>
                    </el-form>
                </edu-dialog>
            </div>
            <!--导入弹框-->
            <!-- <edu-dialog ref="uploadDialog"
                        title="导入专业"
                        :confirmBtnText="$t('common.btn.confirm')"
                        size="small"
                        @close="clearFiles"
                        @confirm="uploadFile">
                <el-form label-width="85px">
                    <el-row>
                        <el-form-item>
                        <el-upload ref="upload"
                                    :data="{}"
                                    :on-success="uploadDone"
                                    :auto-upload="false"
                                    :multiple="false"
                                    :on-change="(file, fileList)=>{ this.fileList = fileList}"
                                    :limit="1"
                                    :accept="'.xlsx'"
                                    :action="importUrl">
                            <el-button slot="trigger"
                                    size="small"
                                    type="primary">{{ $t('label.selectFile') }}
                            </el-button>
                            <div slot="tip"
                                class="el-upload__tip">{{ $t('courseTake.uploadXLSFileOnly') }}
                            </div>
                        </el-upload>
                        </el-form-item>
                    </el-row>
                </el-form>
            </edu-dialog> -->
        </el-card>
    </div>
</template>

<script>
import UTILS from "common/src/utils/utils";
import { queryDic } from "common/src/api/dictionary";
import { getValue } from "common/src/api/commonParam";
import { queryAdvancedLevel } from "common/src/api/minor/collegemajor";
import majorApi from "common/src/api/minor/minorMajorManager";
import {
    queryMajorSupList,
    addMajorSup,
    updateMajorSup,
    deleteMajorSup,
    downloadTemplate,
    exportMajorList,
    queryMajorCode,
    findCategoryList,
    modifyMinorMajorInfoSupIsValid,
    checkMajorRepeatOnCollAndMinorGrade,
    copyMajorBetweenMinorGrades
} from "common/src/api/minor/minorMajorManager";
export default {
    data() {
        var validateCode = (rule, value, callback) => {
            var regex = /^\d{5}$/;
            if (regex.test(value)) {
                callback();
            } else {
                callback(new Error(this.$t("minor.major.plsInputFiveNum")));
            }
        };
        var validateName = (rule, value, callback) => {
            var regex = /^[\u4E00-\u9FA50-9\(\)\s\S]{2,30}$/;
            if (regex.test(value)) {
                callback();
            } else {
                callback(new Error(this.$t("minor.major.wordLimited")));
            }
        };
        return {
            dialogTiltle: false,
            dialogTitle: "",
            managerDeptId: 1,
            optionType: 0, //操作类型：0：新增 1：编辑 2：查看
            tableData: {},
            majorId: 0,
            isUpdate: false,
            importUrl: '',
            options: {
                advancedLevel: [],
                college: [],
                beginSemester: [],
                endSemester: [],
                minorSubjects: [],
                lengthSchoolList: [],
                majorCodeOption: []
            },
            queryParams: {
                grade: "",
                majorCodeOrName: "",
                advancedLevel: "",
                college: ""
            },
            createMajorForm: {
                grade: "",
                college: "",
                advancedLevel: "",
                majorCode: "",
                majorNameCn: "",
                majorNameEn: "",
                beginTerm: "",
                endTerm: "",
                minorSubject: '', //辅修学科门类
                lengthSchooling: ''
            },
            page: {
                pageNum: 1,
                pageSize: 20
            },
            majorRules: {
                grade: [
                    {
                        required: true,
                        message: this.$t("minor.tip.minorGradeSelectTip"),
                        trigger: "blur"
                    }
                ],
                college: [
                    {
                        required: true,
                        message: this.$t('minor.tip.collegeTip'),
                        trigger: "change"
                    }
                ],
                // advancedLevel: [
                //     {
                //         required: true,
                //         message: this.$t("minor.major.plsChooseAdvancedLevel"),
                //         trigger: "change"
                //     }
                // ],
                majorCode: [
                    {
                        required: true,
                        message: this.$t("minor.major.proCodeReq"),
                        trigger: "blur"
                    }
                ],
                majorNameCn: [
                    {
                        required: true,
                        message: this.$t("minor.major.proNameReq"),
                        trigger: "blur"
                    },
                    {
                        validator: validateName,
                        trigger: "blur"
                    }
                ],
                beginTerm: [
                    {
                        required: true,
                        message: this.$t("minor.major.plsChooseBeginSemester"),
                        trigger: "change"
                    }
                ],
                endTerm: [
                    {
                        required: true,
                        message: this.$t("minor.major.plsChooseEndSemester"),
                        trigger: "change"
                    }
                ],
                // minorSubject: [{
                //     required: true,
                //     message: '请选择辅修学科门类',
                //     trigger: "change"
                // }],
                lengthSchooling: [{
                    required: true,
                    message: '请选择学制',
                    trigger: "change"
                }]
            },
            validMap: {
                '0': '启用',
                '1': '停用'
            },
            copyDialog: {
                show: false,
                formInfo: {
                    startMinorGrade: '',
                    endMinorGrade: ''
                },
                rules: {
                    startMinorGrade: [{
                        required: true,
                        message: '请选择'
                    }],
                    endMinorGrade: [{
                        required: true,
                        message: '请选择'
                    }]
                }
            }
        };
    },
    created() {},
    mounted() {
        if (!this.$checkMinor()) return;
        this.getOptions();
        this.initAdvancedLevel();
        //初始化查询专业列表
        this.initMajorList();
        //初始化学期
        this.initSemester();
    },
    activated() {},
    methods: {
        getOptions() {
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX", "X_XZ"]
            };
            queryDic(data).then(res => {
                this.options.college = [];
                UTILS.fillSelect(
                    {
                        college: "X_YX",
                        lengthSchoolList: "X_XZ"
                    },
                    res.data,
                    this.options
                );
            });
            findCategoryList().then(res => {
                if (res && res.code == 200) {
                    let option = [];
                    (res.data || []).forEach(item => {
                        option.push({
                            label: `${item.name}(${item.code})`,
                            value: item.code
                        })
                    })
                    this.options.minorSubjects = option;
                }
            })
        },
        initAdvancedLevel(isOnCampus) {
            let params = {
                isOnCampus: "0"
            };
            this.options["advancedLevel"] = [];
            queryAdvancedLevel(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.length; key++) {
                        this.options["advancedLevel"].push({
                            value: res.data[key].advancedLevelCode,
                            label: res.data[key].advancedLevelName
                        });
                    }
                }
            });
        },
        initMajorList() {
            if (!this.$checkMinor()) return;
            let params = {
                minorGrade: this.queryParams.grade,
                collegeCode: this.queryParams.college,
                advancedCode: this.queryParams.advancedLevel,
                keyWord: this.queryParams.majorCodeOrName,
                page: this.page.pageNum,
                pageSize: this.page.pageSize
            };
            this.queryMajorList(params);
        },
        queryMajorList(params) {
            if (!this.$checkMinor()) return;
            queryMajorSupList(params).then(res => {
                if (res.code == 200) {
                    const dataList = res.data;
                    if (dataList.list && dataList.list.length > 0) {
                        dataList.list.forEach(e => {
                            e.beginTerm = !e.beginTerm
                                ? null
                                : `第${this.$utils.numberToChinese(
                                      e.beginTerm
                                  )}学期`;
                            e.endTerm = !e.endTerm
                                ? null
                                : `第${this.$utils.numberToChinese(
                                      e.endTerm
                                  )}学期`;
                        });
                    } else {
                        dataList.list = [];
                    }
                    this.tableData = dataList;
                } else {
                    throw response.msg;
                }
            });
        },
        initSemester() {
            if (this.options.beginSemester.length > 0) return;
            let years = [];
            for (let i = 1; i < 11; i++) {
                years.push({
                    label: this.getSemester(i),
                    value: Number(i)
                });
            }
            this.options.beginSemester = years;
            this.options.endSemester = years;
        },
        getSemester(n) {
            if (n) return `第${this.$utils.numberToChinese(n)}学期`;
            else return this.$t("minor.major.plsChooseTrem");
        },
        // handleSemesterChange1(type) {
        //     if (
        //         this.createMajorForm.endTerm &&
        //         this.createMajorForm.beginTerm &&
        //         this.createMajorForm.beginTerm >= this.createMajorForm.endTerm
        //     ) {
        //         this.$message({
        //             type: "warning",
        //             message: this.$t("minor.major.plsChooseTrem")
        //         });
        //         if (type == 1) this.createMajorForm.beginTerm = "";
        //         else this.createMajorForm.endTerm = "";
        //     }
        // },
        addMajorInfo() {
            if (!this.$checkMinor()) return;
            this.createMajorForm = {
                grade: "",
                college: "",
                advancedLevel: "",
                majorCode: "",
                majorNameCn: "",
                majorNameEn: "",
                beginTerm: "",
                endTerm: "",
                lengthSchooling: '',
                minorSubject: ''
            };
            this.$refs.createMajorForm &&
                this.$refs.createMajorForm.resetFields();
            this.optionType = 0;
            this.isUpdate = false;
            this.dialogTitle = this.$t("minor.major.addMajorTitle");
            this.$refs.addDialog.open();
        },
        resetQuery() {
            this.queryParams = {
                grade: "",
                advancedLevel: "",
                college: ""
            };
            this.initMajorList();
        },
        searchData() {
            this.page.pageNum = 1;
            let params = {
                minorGrade: this.queryParams.grade,
                collegeCode: this.queryParams.college,
                advancedCode: this.queryParams.advancedLevel,
                keyWord: this.queryParams.majorCodeOrName,
                page: this.page.pageNum,
                pageSize: this.page.pageSize
            };
            this.queryMajorList(params);
        },
        search() {
            this.searchData();
            // this.page.pageNum = 1;
            // let params = {
            //     minorGrade: this.queryParams.grade,
            //     collegeCode: this.queryParams.college,
            //     advancedLevelCode: this.queryParams.advancedLevel,
            //     keyWord: this.queryParams.majorCodeOrName,
            //     page: this.page.pageNum,
            //     pageSize: this.page.pageSize
            // };
            // this.queryMajorList(params);
        },
        viewRecord(row) {
            this.createMajorForm = {
                grade: "",
                college: "",
                advancedLevel: "",
                majorCode: "",
                majorNameCn: "",
                majorNameEn: "",
                beginTerm: "",
                endTerm: "",
                lengthSchooling: '',
                minorSubject: '',
            };
            this.$refs.createMajorForm &&
                this.$refs.createMajorForm.resetFields();
            this.optionType = 2;
            this.isUpdate = true;
            this.assignmentForm(row);
            this.dialogTitle = this.$t("minor.major.viewMajorTitle");
            this.$refs.addDialog.open();
        },
        editlRecord(row) {
            this.createMajorForm = {
                grade: "",
                college: "",
                advancedLevel: "",
                majorCode: "",
                majorNameCn: "",
                majorNameEn: "",
                beginTerm: "",
                endTerm: "",
                lengthSchooling: '',
                minorSubject: '',

            };
            this.$refs.createMajorForm &&
                this.$refs.createMajorForm.resetFields();
            this.optionType = 1;
            this.isUpdate = false;
            this.majorId = row.id;
            this.assignmentForm(row);
            this.dialogTitle = this.$t("minor.major.editMajorTitle");
            this.$refs.addDialog.open();
        },
        deleteRecord(row) {
            let params = {
                majorId: row.id
            };

            this.$confirm(
                this.$t("minor.major.deleteMajorSign"),
                this.$t("message.prompt"),
                {
                    confirmButtonText: this.$t("message.confirm"),
                    cancelButtonText: this.$t("message.cancel"),
                    type: "warning"
                }
            ).then(() => {
                deleteMajorSup(params).then(res => {
                    if (res.code === 200) {
                        //this.$refs.addDialog.close();
                        this.$message({
                            message: "删除成功",
                            type: "success"
                        });
                        this.searchData();
                    }
                });
            });
        },
        pageChange(page) {
            this.page.pageNum = page.pageNum;
            this.page.pageSize = page.pageSize;
            this.initMajorList();
        },
        cancelMajorForm() {
            this.$refs.addDialog.close();
        },
        //校验英文名称只能是字母、数字或者空格
        checkNameEn(value) {
            if (
                value &&
                value.toString().replace(/^[a-zA-Z\d\s]*$/, "") != ""
            ) {
                this.$message.error(this.$t("minor.tip.englishNameError"));
                return false;
            } else {
                return true;
            }
        },
        submitMajorForm() {
            this.$refs.createMajorForm.validate(valid => {
                if (valid) {
                    //校验英文名称
                    if (!this.checkNameEn(this.createMajorForm.majorNameEn)) {
                        return;
                    }
                    let params = {
                        minorGrade: this.createMajorForm.grade,
                        beginCollege: this.createMajorForm.college,
                        advancedLevelCode: this.createMajorForm.advancedLevel,
                        majorCode: this.createMajorForm.majorCode,
                        majorNameCn: this.createMajorForm.majorNameCn,
                        majorNameEn: this.createMajorForm.majorNameEn,
                        beginTerm: this.createMajorForm.beginTerm,
                        endTerm: this.createMajorForm.endTerm,
                        courseSubjectClass: this.createMajorForm.minorSubject,
                        lengthOfSchooling: this.createMajorForm.lengthSchooling
                    };
                    if (this.optionType === 0) {
                        //新增数据提交
                        addMajorSup(params).then(res => {
                            if (res.code === 200) {
                                this.$refs.addDialog.close();
                                this.searchData();
                                this.$message({
                                    message: "新增成功",
                                    type: "success"
                                });
                            }
                        });
                    }
                    if (this.optionType === 1) {
                        //编辑数据提交
                        if (!!this.majorId) {
                            params.id = this.majorId;
                            updateMajorSup(params).then(res => {
                                if (res.code === 200) {
                                    this.$refs.addDialog.close();
                                    this.searchData();
                                    this.$message({
                                        message: "编辑成功",
                                        type: "success"
                                    });
                                }
                            });
                        }
                    }
                } else {
                    return false;
                }
            });
        },
        assignmentForm(row) {
            const map = new Map();
            map.set(1, "第一学期");
            map.set(2, "第二学期");
            map.set(3, "第三学期");
            map.set(4, "第四学期");
            map.set(5, "第五学期");
            map.set(6, "第六学期");
            map.set(7, "第七学期");
            map.set(8, "第八学期");
            map.set(9, "第九学期");
            map.set(10, "第十学期");
            map.forEach((item, index, arr) => {
                if (item == row.beginTerm) {
                    this.createMajorForm.beginTerm = index;
                }
                if (item == row.endTerm) {
                    this.createMajorForm.endTerm = index;
                }
            });
            this.createMajorForm.grade = row.minorGrade + "";
            this.createMajorForm.college = row.beginCollege;
            this.createMajorForm.advancedLevel = row.advancedLevelCode;
            this.createMajorForm.majorCode = row.majorCode;
            this.createMajorForm.majorNameCn = row.majorNameCn;
            this.createMajorForm.majorNameEn = row.majorNameEn;
            this.createMajorForm.minorSubject = row.courseSubjectClass;    
            this.createMajorForm.lengthSchooling = row.lengthOfSchooling;
        },
        //导出excel
        exportExcel() {
            if (!this.$checkMinor()) return;
            let idStr = '';
            if (this.multiSelectData && this.multiSelectData.length) {
                idStr = this.multiSelectData.map(item => item.id).join(',');
            }
            let params = {
                minorGrade: this.queryParams.grade,
                advanceLevel: this.queryParams.advancedLevel,
                collegeCode: this.queryParams.college,
                keyWord: this.queryParams.majorCodeOrName,
                idsStr: idStr
            };
            let url = exportMajorList(params);
            window.open(url);
        },
        exportFile() {
            this.importUrl = '';
            this.$refs.uploadDialog.open();
            //this.importUrl = 'xxx';
        },
        // 导入按钮组操作
        handleImportCommand(command) {
            if (command === "download") {
                majorApi.downloadTemplate();
            } else if (command === "import") {
                this.$refs.importBtn.click();
            }
        },
        // 文件导入成功
        handlerSuccess(response, file, fileList) {
            let responseCode = response.code;
            if (responseCode === 200) {
                this.$message({
                    message: "上传成功！",
                    type: "success"
                });
            } else {
                this.$message({
                    message: response.msg,
                    type: "error"
                });
            }
            this.searchList();
            this.$refs.upload.clearFiles();
        },
        // 文件导入错误
        handlerError(response, file, fileList) {
            this.$message({
                message: "上传失败！",
                type: "error"
            });
            if (this.uploadType) {
                this.$refs.uploadEdit.clearFiles();
                return;
            }
            this.$refs.upload.clearFiles();
        },
        beforeAvatarUpload(file) {
            //上传附件的校验
                let requireFile = [
                    "xlsx",
                    "xls"
                ];
                const fileFormate =
                    requireFile.indexOf(
                        file.name.split(".")[file.name.split(".").length - 1]
                    ) > -1;
                if (!fileFormate) {
                    this.$message({
                        message: '只能上传xlsx类型的文件',
                        type: "warning"
                    });
                }
                if (!(fileFormate)) {
                    //先清空附件
                    return false;
                }
        },
        uploadFile() {
			if (_.isEmpty(this.fileList)) {
				this.$message({
					type: "info",
					message: this.$t("message.selectFilePlease")
				});
				return;
			}
			this.$refs.upload.submit();
        },
        // 关闭弹出框
        clearFiles() {
            this.$refs.upload.clearFiles();
            this.fileList = [];
            this.$refs.uploadDialog.close();
        },
        // 导入成功
        uploadDone(res) {
            if (res.code == 200) {
                this.$message({
                    type: 'success',
                    message: this.$t('common.tips.uploadSuccess')
                });
                this.handleSearch();
            }else{
                this.$message({
                    type: 'error',
                    message: res.msg
                });
                return 
            }
            this.clearFiles();
        },
        queryMajorCode() {
            if (this.createMajorForm.grade && this.createMajorForm.college) {
                this.createMajorForm.majorCode = '';
                let params = {
                    collegeCode: this.createMajorForm.college,
                    minorGrade: this.createMajorForm.grade
                }
                queryMajorCode(params).then(res => {
                    if (res && res.code == 200) {
                        res.data && (this.createMajorForm.majorCode = res.data);
                    }
                })
            }
        },
        dialogClose() {
            this.$refs.createMajorForm && this.$refs.createMajorForm.resetFields();
        },
        setValid({id, isValid}) {
            let param = {
                majorId: id,
                isValid: isValid == 0 ? 1 : 0
            }
            modifyMinorMajorInfoSupIsValid(param).then(res =>{
                if (res && res.code == 200) {
                     this.$message({
                        type: 'success',
                        message: '设置成功'
                    });
                    this.searchData();
                }
            })
        },
        copyInfo() {
            this.copyDialog.show = true;
        },
        copyDialogConfirm() {
            this.$refs.copyForm.validate(async (valid) => {
                if (valid) {
                    let param = {...this.copyDialog.formInfo};
                    if (param.startMinorGrade == param.endMinorGrade) {
                         this.$message({
                            type: 'error',
                            message: '源专业年级和要复制到的专业年级不能相同'
                        });
                        return;
                    }
                    let {data, code} = await checkMajorRepeatOnCollAndMinorGrade(param);
                    if (code == 200) {
                        if (data) {
                            this.$eduConfirm('已存在相同的专业，是否覆盖？','提示').then(() => {
                                this.copyMajorBetweenMinorGrades(param);
                            })
                        }
                        else {
                            this.copyMajorBetweenMinorGrades(param);
                        }
                    }
                }else {
                    return false;
                }
            })
        },
        copyMajorBetweenMinorGrades(param) {
            copyMajorBetweenMinorGrades(param).then(res => {
                if (res && res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '复制成功'
                    });
                    this.copyDialogClose();
                    this.searchData();
                }
            })
        },
        copyDialogClose() {
            this.$refs.copyForm.resetFields();
            this.copyDialog.show = false;
        },
        handleSelectionChange(val) {
            this.multiSelectData = val;
        }
    }
};
</script>

<style scoped>
.first_title {
    font-size: 18px;
    font-family: "微软雅黑";
    color: #2b3b4e;
    padding: 0px 0px 16px;
}
.btn {
    text-align: right;
    padding: 0px 0px 16px;
}
</style>
